﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TinyForum.DataObjects;
using Apworks.Repositories;
using TinyForum.Infrastructure;
using TinyForum.Domain.Model;
using Apworks.Specifications;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;

namespace TinyForum.Application.Implementation
{
    public class SiteApplication : ISiteApplication
    {
        private readonly ICacheManager cacheManager = CacheFactory.GetCacheManager("DefaultCacheManager");
        private readonly string siteSettingCacheKey = typeof(SettingDataObject).FullName;

        #region ISiteApplication Members

        public SettingDataObject GetSiteSettings()
        {
            if (cacheManager.Contains(siteSettingCacheKey))
            {
                return cacheManager[siteSettingCacheKey] as SettingDataObject;
            }
            else
            {
                using (RepositoryContextManager mgr = new RepositoryContextManager())
                {
                    IRepository<Setting> settingRepository = mgr.GetRepository<Setting>();
                    IEnumerable<Setting> settings = settingRepository.FindAll();
                    SettingDataObject settingDataObject = null;
                    if (settings == null || settings.Count() == 0)
                    {
                        settingDataObject = DataObjectMapper.MapToDataObject(Setting.Default);
                    }
                    else
                    {
                        settingDataObject = DataObjectMapper.MapToDataObject(settings.First());
                    }
                    cacheManager.Add(siteSettingCacheKey, settingDataObject);
                    return settingDataObject;
                }
            }
        }

        public void SaveSiteSettings(SettingDataObject setting)
        {
            using (RepositoryContextManager mgr = new RepositoryContextManager())
            {
                IRepository<Setting> settingRepository = mgr.GetRepository<Setting>();
                ISpecification<Setting> specification = Specification<Setting>.Eval(p => p.ID != 0);
                bool settingExists = settingRepository.Exists(specification);
                Setting s = null;
                if (settingExists)
                {
                    s = settingRepository.Find(specification);
                    if (setting.PostsPerPage != null)
                        s.PostsPerPage = setting.PostsPerPage;
                    if (setting.RepliesPerPage != null)
                        s.RepliesPerPage = setting.RepliesPerPage;
                    if (setting.SiteCopyright != null)
                        s.SiteCopyright = setting.SiteCopyright;
                    if (setting.SiteDescription != null)
                        s.SiteDescription = setting.SiteDescription;
                    if (setting.SiteTheme != null)
                        s.SiteTheme = setting.SiteTheme;
                    if (setting.SiteTitle != null)
                        s.SiteTitle = setting.SiteTitle;
                }
                else
                {
                    s = new Setting
                    {
                        PostsPerPage = setting.PostsPerPage,
                        RepliesPerPage = setting.RepliesPerPage,
                        SiteCopyright = setting.SiteCopyright,
                        SiteDescription = setting.SiteDescription,
                        SiteTitle = setting.SiteTitle,
                        SiteTheme = setting.SiteTheme
                    };
                }
                settingRepository.Add(s);
                mgr.Commit();
                if (cacheManager.Contains(siteSettingCacheKey))
                    cacheManager.Remove(siteSettingCacheKey);
            }
        }

        #endregion
    }
}
